package com.ruoyi.internet.controller;

import com.ruoyi.common.core.constant.HttpStatus;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.internet.api.domain.IntUser;
import com.ruoyi.internet.service.IIntUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 互联网用户Controller
 *
 * @author cjie
 * @date 2021-08-16
 */
@Api(tags = " 互联网用户")
@RestController
@RequestMapping("/intUser")
public class IntUserController extends BaseController {
    @Autowired
    private IIntUserService intUserService;

    /**
     * 查询互联网用户列表
     */
    @ApiOperation(value = "查询互联网用户列表", notes = "查询互联网用户列表")
    @GetMapping("/list")
    public TableDataInfo list(IntUser intUser) {
        startPage();
        List<IntUser> list = intUserService.selectIntUserList(intUser);
        return getDataTable(list);
    }

    /**
     * 通过用户名或电话查询用户(后台调用)
     *
     * @param intUser 互联网用户
     * @return
     */
    @PostMapping("/info")
    public R<IntUser> info(@RequestBody IntUser intUser) {
        IntUser user = intUserService.selectIntUserByUsernameOrPhone(intUser);
        if (StringUtils.isNull(user)) {
            return R.fail("用户名或密码错误");
        }
        return R.ok(user);
    }

    /**
     * 导出互联网用户列表
     */
    @ApiOperation(value = "导出互联网用户列表", notes = "导出互联网用户列表")
    @Log(title = "互联网用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IntUser intUser) throws IOException {
        List<IntUser> list = intUserService.selectIntUserList(intUser);
        ExcelUtil<IntUser> util = new ExcelUtil<IntUser>(IntUser.class);
        util.exportExcel(response, list, "互联网用户数据");
    }

    /**
     * 获取互联网用户详细信息
     */
    @ApiOperation(value = "获取互联网用户详细信息", notes = "获取互联网用户详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(intUserService.selectIntUserById(id));
    }

    /**
     * 获取登录用户
     */
    @ApiOperation(value = "获取登录用户", notes = "获取登录用户")
    @GetMapping(value = "/loginInfo")
    public AjaxResult getLoginUserInfo() {
        if (SecurityUtils.getUserId() != null) {
            return AjaxResult.success(intUserService.selectIntUserById(SecurityUtils.getUserId()));
        }
        return AjaxResult.error(HttpStatus.UNAUTHORIZED,"未登录");
    }


    /**
     * 互联网用户注册
     */
    @ApiOperation(value = "互联网用户注册", notes = "互联网用户注册")
    @Log(title = "互联网用户", businessType = BusinessType.INSERT)
    @PostMapping("/register")
    public AjaxResult add(@RequestBody IntUser intUser) {
        return intUserService.insertIntUser(intUser);
    }

    /**
     * 修改密码
     */
    @ApiOperation(value = "修改密码", notes = "修改密码")
    @Log(title = "修改密码", businessType = BusinessType.UPDATE)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "username", dataType = "String", required = true, value = "用户名"),
            @ApiImplicitParam(paramType = "query", name = "oldPwd", dataType = "String", required = true, value = "原始密码"),
            @ApiImplicitParam(paramType = "query", name = "pwd", dataType = "String", required = true, value = "新密码")
    })
    @PostMapping("/updatePwd")
    public AjaxResult updatePwd(String username, String oldPwd, String pwd) {
        return intUserService.updatePwd(username, oldPwd, pwd);
    }

    /**
     * 找回密码
     */
    @ApiOperation(value = "找回密码", notes = "找回密码")
    @Log(title = "找回密码", businessType = BusinessType.UPDATE)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "phone", dataType = "String", required = true, value = "电话"),
            @ApiImplicitParam(paramType = "query", name = "code", dataType = "String", required = true, value = "验证码"),
            @ApiImplicitParam(paramType = "query", name = "pwd", dataType = "String", required = true, value = "密码")
    })
    @PostMapping("/findPwd")
    public AjaxResult findPwd(String phone, String code, String pwd) {
        return intUserService.updatePwd(phone, code, pwd);
    }

    /**
     * 修改互联网用户
     */
    @ApiOperation(value = "修改互联网用户", notes = "修改互联网用户")
    @Log(title = "互联网用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IntUser intUser) {
        return toAjax(intUserService.updateIntUser(intUser));
    }

    /**
     * 删除互联网用户
     */
    @ApiOperation(value = "删除互联网用户", notes = "删除互联网用户")
    @ApiImplicitParam(name = "ids", value = "互联网用户ID数组", required = true, paramType = "path", allowMultiple = true, dataType = "String")
    @Log(title = "互联网用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(intUserService.deleteIntUserByIds(ids));
    }
}
